// Copyright (c)  YISH. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using NSQL.Expressions;
using NSQL.Parser;

namespace NSQL
{
    public class JoinBuilder
    {
        private readonly IJoinable _base;
        private readonly IJoinable _to;
        private readonly JoinType _type;
        public static JoinBuilder Join(IJoinable baseTable, IJoinable joinTable, JoinType type) => new JoinBuilder(baseTable, joinTable, type);
        public static JoinBuilder FullJoin(IJoinable baseTable, IJoinable joinTable) => new JoinBuilder(baseTable, joinTable, JoinType.FullJoin);
        public static JoinBuilder LeftJoin(IJoinable baseTable, IJoinable joinTable) => new JoinBuilder(baseTable, joinTable, JoinType.Left);
        public static JoinBuilder RightJoin(IJoinable baseTable, IJoinable joinTable) => new JoinBuilder(baseTable, joinTable, JoinType.Right);
        public static JoinBuilder InnerJoin(IJoinable baseTable, IJoinable joinTable) => new JoinBuilder(baseTable, joinTable, JoinType.Inner);
        public static JoinBuilder CrossJoin(IJoinable baseTable, IJoinable joinTable) => new JoinBuilder(baseTable, joinTable, JoinType.CrossJoin);
        public static JoinBuilder LeftOuterJoin(IJoinable baseTable, IJoinable joinTable) => new JoinBuilder(baseTable, joinTable, JoinType.LeftOuter);
        public static JoinBuilder RightOuterJoin(IJoinable baseTable, IJoinable joinTable) => new JoinBuilder(baseTable, joinTable, JoinType.RightOuter);


        private JoinBuilder(IJoinable @base, IJoinable to, JoinType type)
        {
            _base = @base;
            _to = to;
            _type = type;
        }

        public JoinNode On(IPredicateExpression condition)
        {
            return new JoinNode(_base, _to, _type, condition);
        }

        public JoinNode On(string condition) => On(SqlParser.ParsePredicateExpression(condition));
    }
}